package com.yc.hotel.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * Redis支持的数据类型:
 * string:二进制安全存储，session共享、分布式锁、计数器、限流
 * list:消息队列、文章列表
 * hash :缓存用户信息、购物车
 * set:不重复，用户标签、随机抽奖、社交
 * zset:已排序字符串集合，排行榜、用户点赞
 * @author 22408
 *
 */
@Configuration
public class RedisConfig {
	/**
	 * 重写redis序列化方式，使用json
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		
		//创建json序列化对象
		GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
		
		//创建字符串的序列化对象
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		
		redisTemplate.setKeySerializer(stringRedisSerializer);
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.afterPropertiesSet();
		
		return redisTemplate;
	}
}
